<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Department;
use App\Models\Doctor;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class DoctorController extends Controller
{
    // 列表
    public function index(Request $request)
    {
        $searchData = $request->post();

        $model = Doctor::orderBy('doctor.id', 'DESC')
            ->leftJoin('users', 'users.id', '=', 'doctor.user_id')
            ->select([
                'doctor.*', 'users.nickname'
            ]);

        if ($nickname = $searchData['nickname'] ?? '') {
            $model->where('nickname', 'like', "%{$nickname}%");
        }
        if ($department_id = $searchData['department_id'] ?? '') {
            $model->where('department_id', $department_id);
        }
        if ($name = $searchData['name'] ?? '') {
            $model->where('doctor.name', 'like', "%{$name}%");
        }
        if ($hospital = $searchData['hospital'] ?? '') {
            $model->where('hospital', 'like', "%{$hospital}%");
        }
        if ($phone = $searchData['phone'] ?? '') {
            $model->where('doctor.phone', 'like', "%{$phone}%");
        }
        if ($id_num = $searchData['id_num'] ?? '') {
            $model->where('doctor.id_num', 'like', "%{$id_num}%");
        }

        $result = $model->paginate(config('app.limit'));
        foreach ($result->items() as $item) {
            $item->createdAt = $item->created_at->format('Y-m-d H:i:s');
        }

        return $this->writeJson(0, [
            'data' => $result->items(),
            'count' => $result->total(),
            'departmentList' => Department::getListAll()
        ]);
    }

    // 通过审核
    public function agree(Request $request)
    {
        $id = $request->post('id');

        $doctor = Doctor::find($id);
        if ($doctor->status != 0) {
            return $this->writeJson(500, null, '状态错误');
        }

        try {
            DB::beginTransaction();
            // 修改用户身为为专家
            User::whereId(auth('wechat')->id())
                ->update([
                    'is_doctor' => 1
                ]);
            $doctor->status = 1;
            $doctor->save();

            DB::commit();
            return $this->writeJson(0, null, '操作成功');
        } catch (\Exception $exception) {
            DB::rollBack();
            \Log::error($exception);
        } catch (\Throwable $throwable) {
            DB::rollBack();
            \Log::error($throwable);
        }
        return $this->writeJson(500, null, '操作失败');
    }

    // 拒绝
    public function reject(Request $request)
    {
        $id = $request->post('id');

        $doctor = Doctor::find($id);
        if ($doctor->status != 0) {
            return $this->writeJson(500, null, '状态错误');
        }
        $doctor->status = 2;
        if ($doctor->save()) {
            return $this->writeJson(0, null, '操作成功');
        } else {
            return $this->writeJson(500, null, '操作失败');
        }
    }

    // 删除
    public function del(Request $request)
    {
        $id = $request->post('id');
        if (empty($id)) {
            return $this->writeJson(500, null, '参数错误');
        }
        Doctor::destroy($id);
        return $this->writeJson(0, null, '删除成功');
    }
}
